Skip to content

PostgreSQL: Support SQL standard ARRAY keyword in type declarations#2356

Open
LucaCappelletti94 wants to merge 8 commits into
apache:mainfrom
LucaCappelletti94:array
Open

PostgreSQL: Support SQL standard ARRAY keyword in type declarations#2356
LucaCappelletti94 wants to merge 8 commits into
apache:mainfrom
LucaCappelletti94:array

Conversation

@LucaCappelletti94

Copy link
Copy Markdown
Contributor

Adds support for the SQL standard ARRAY keyword in array type declarations, such as INTEGER ARRAY and INTEGER ARRAY[4], which PostgreSQL accepts anywhere a type is expected (column definitions, casts, and the :: operator). It is gated behind a new Dialect::supports_array_typedef_with_keyword method, enabled for PostgreSQL, and the keyword form with its optional cardinality is represented by a new ArrayElemTypeDef::Keyword variant.

This also unifies the existing MySQL multi-valued index syntax CAST(... AS UNSIGNED ARRAY). The array: bool field on Expr::Cast is removed and the trailing ARRAY keyword is now captured through ArrayElemTypeDef::Keyword for every dialect, so this is a breaking change to the AST. Parser behavior is unchanged for all dialects, only the representation is unified, and round-trip serialization is preserved.

@Rattenkrieg

Copy link
Copy Markdown

Hit this same limitation independently: production Flyway migrations using the SQL-standard keyword form on a user-defined type (ALTER TABLE t ADD COLUMN c currency ARRAY). Tested this branch against those migrations plus adjacent shapes (INTEGER ARRAY, TEXT ARRAY[4], NUMERIC(10,2) ARRAY, public.currency ARRAY, INT ARRAY DEFAULT ARRAY[]::INT[]) - all parse and round-trip correctly. Two suggestions:

1. Enable it for GenericDialect. The README asks for dialect-specific syntax to be accepted by both the relevant dialect and GenericDialect, and the bracket form's supports_array_typedef_with_brackets is already enabled for generic, so the keyword form should follow:

 // src/dialect/generic.rs
+    fn supports_array_typedef_with_keyword(&self) -> bool {
+        true
+    }

2. Widen the tests. The current tests cover built-in element types in CREATE TABLE and CAST. These cases exercise different paths (custom and schema-qualified type names, ALTER TABLE, parenthesized typmods, and the suffix-vs-constructor ambiguity) and are worth pinning:

pg_and_generic().verified_stmt("CREATE TABLE t (c currency ARRAY)");
pg_and_generic().verified_stmt("CREATE TABLE t (c public.currency ARRAY)");
pg_and_generic().verified_stmt("ALTER TABLE t ADD COLUMN c currency ARRAY");
pg_and_generic().verified_stmt("CREATE TABLE t (c NUMERIC(10,2) ARRAY)");
pg_and_generic().verified_stmt("CREATE TABLE t (c INT ARRAY DEFAULT ARRAY[]::INT[])");

(pg() instead of pg_and_generic() if you'd rather keep the generic change separate.)

Can send both as a patch against your branch if that's easier.

@LucaCappelletti94

Copy link
Copy Markdown
Contributor Author

@iffyio could you check this out when you have time?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants